package actionHandlers;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class ArbShapeDialogBox extends JDialog implements ActionListener {

	private String name;
  	private JTextField nameField = new JTextField(15);
    private JButton btnOK = new JButton("   OK   ");
    private JButton btnCancel = new JButton("Cancel");
	  /**********************************************************
	   * This creates the diag box that gets the row and col inputs.
	   *************************************************************/
	public ArbShapeDialogBox(){
		setLayout(new BorderLayout());
	    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    setLocation(400,300);
	    
	    //format the box
	    add(new JLabel("Give This Shape A Name:", JLabel.CENTER),BorderLayout.NORTH);
	    //all the center
	    JPanel display = new JPanel();
	    display.setLayout(new GridLayout(2,1));
  
	    JPanel namePanel = new JPanel();
	    namePanel.add(new JLabel("Name: ",JLabel.LEFT));
	    namePanel.add(nameField);
	    display.add(namePanel);
	    add(display, BorderLayout.CENTER);
	    //add bottom
	    JPanel buttonPanel = new JPanel();
	    btnOK.addActionListener(this);
	    //btnOK.addKeyListener(l)
	    btnCancel.addActionListener(this);
	    buttonPanel.add(btnOK);
	    buttonPanel.add(btnCancel);
	    add(buttonPanel,BorderLayout.SOUTH);
	    //pack();
	}
	
	public String showDialog(){
		nameField.setText("");
	    pack();
		setModal(true);
		setVisible(true);
		return name;
	}
		  
		/**********************************************************
		 *   This is used by the new garden maker, to ask the usr
		 *     the dimensions of the new garden.  
		 ********************************************************/
		public void actionPerformed(ActionEvent ae)
	    {
		  if(ae.getSource() == btnCancel){
		    	  name = null;
		    	  dispose();
		  }
		  else if(ae.getSource() == btnOK){
	    	  try{  		  
	    		  name =nameField.getText().trim();
	    		  dispose();
	    		  
	    		  
	  	      }
	  	      catch (NumberFormatException nfe)
	  	      {   
	  	    	System.out.println("NumberFormatException: " + nfe.getMessage()); 
	  	    	dispose();
	  	      }
	  	      catch (IllegalArgumentException e){
	  	    	//this is thrown when the diag box is X's out of..
	  	    	 dispose();
	  	      }
	    	  
	      }

	      
	      dispose();
	    }

	
}
